﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AliyunOpenSearch4Net
{
    internal static partial class Extensions
    {
        public static string[] Split(this string source, char[] seperator, int count)
        {
            var list = new List<string>(count);

            var chars = source.ToCharArray();
            var startPos = 0;

            while(true)
            {
                var added = false;

                for(var i = startPos; i < source.Length; i++)
                {
                    var c = source[i];

                    if (seperator.Contains(c))
                    {
                        list.Add(new string(chars, startPos, i - startPos));
                        startPos = i + 1;
                        added = true;
                        break;
                    }
                }

                if (added && list.Count < (count - 1))
                    continue;

                list.Add(new string(chars, startPos, source.Length - startPos - 1));
                break;
            }

            return list.ToArray();
        }
    }
}
